﻿#define _CRT_SECURE_NO_WARNINGS
// ↑ 将本行在代码放在第一行可以避免 scanf的报错问题（仅在VS编译器里）
//【初阶】15.vector(1)

//【头文件包含】
//C++的主要头文件



#include<iostream>//c++的输入输出流
using std::cout, std::endl, std::cin;
#include<vector>
using std::vector;
//C的主要头文件
//#include<stdio.h>
//#include<stdlib.h>
//#include<assert.h>
//#include<string.h> 




//【命名空间】

//【结构体声明】【类声明】



//【函数声明】
void Test1();void Test2();void Test3();
void printvector(const vector<int>& v);
void rprintvector(const vector<int>& v);


//【主函数】
int main()
{
	
	Test1();
	Test2();
	Test3();
	
	cout << "****主函数结束****" << endl;
	return 0;
}

//【函数定义】
/*只读迭代器测试*/ void printvector(const vector<int>& v)
{
	vector<int>::const_iterator cit=v.cbegin();
	while (cit != v.cend())
	{
		cout << *cit << " ";
		++cit;
	}
	cout << endl;
}
/*只读反向迭代器测试*/ void rprintvector(const vector<int>& v)
{
	vector<int>::const_reverse_iterator crit = v.crbegin();
	while (crit != v.crend())
	{
		cout << *crit << " ";
		++crit;
	}
	cout << endl;
}
/*测试用例*/void Test1()
{

	cout << "****测试用例开始****"<< endl;
	/*默认构造*/
	vector<int> v1;
	/*尾插*/
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	/*普通遍历*/
	for (size_t i = 0; i < v1.size(); ++i)
	{
		cout << v1[i] << " ";
	}
	cout << endl;

	/*拷贝构造*/
	vector<int>v2(v1);
	/*迭代器遍历*/
	vector<int>::iterator it = v2.begin();
	while (it != v2.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	vector<int> v3;
	v3.push_back(5);
	v3.push_back(6);
	v3.push_back(7);
	v3.push_back(8);
	/*赋值*/
	v1 = v3;
	/*auto遍历*/
	for (auto a1 : v1)
	{
		cout << a1 << " ";
	}
	cout << endl;

	/*反向迭代器遍历*/
	vector<int>::reverse_iterator rit = v3.rbegin();
	while (rit != v3.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
	/*只读const迭代器遍历*/
	printvector(v3);
	/*只读const反向迭代器遍历*/
	rprintvector(v3);

	cout << "****测试用例结束****" << endl << endl;

}
/*测试用例*/void Test2()
{
	cout << "****测试用例开始****" << endl;
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	/*容量相关成员函数*/
	cout << v1.size() << " " << v1.capacity() << endl;
	v1.push_back(5);
	cout << v1.size() << " " << v1.capacity() << endl;
	v1.push_back(6);
	v1.push_back(7);
	cout << v1.size() << " " << v1.capacity() << endl;
	/*at和[]*/
	cout << v1[6] << endl;
	cout << v1.at(6) << endl;
	/*insert和erase*/
	/*头插*/
	v1.insert(v1.begin(), 0);
	cout << v1[0]<< endl;
	/*头删*/
	v1.erase(v1.begin());
	cout << v1[0] << endl;

	/*std的找算法实现vector元素定向*/
	using std::find;
	vector<int>::iterator pos = find(v1.begin(), v1.end(), 5);
	if (pos != v1.end())
	{
		v1.erase(pos);
	}
	for (auto a1 : v1)
	{
		cout << a1 << " ";
	}
	cout << endl;
	cout << "****测试用例结束****" << endl<<endl;
}
/*测试用例*/void Test3()
{
	cout << "****测试用例开始****" << endl;


	cout << "****测试用例结束****" << endl << endl;
}

//【笔记】
//【初阶】15.vector(1)
	//vector:是表示可变大小数组的序列容器，动态分配数组
		//类似数组（或顺序表）、大小可动态改变且大小会被容器自动处理
	//vector主要成员函数
		//默认：构造、拷贝构造、析构
		//迭代器：普通iterator、反向reverse_iterator、只读const_iterator、只读反向const_reverse_iterator
		//容量：size()、capacity()，容量大约按1.5倍扩容
		//[]和at：[]越界报错，at越界抛异常
		//insert和erase：
		//
		// 




